﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using RestSharpEx;
using ShareTools_Auth;

namespace HttpClientT
{
    public partial class MainForm : Form
    {
        RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
        byte[] key_data = new byte[32];

        private SynchronizationContext _ctx = null;
        public MainForm()
        {
            InitializeComponent();
        }

        private void GenKey()
        {
            crypto.GetNonZeroBytes(key_data);
            txtKeyGen.Text = key_data.ToBase62String();
            System.Diagnostics.Debug.WriteLine(key_data.ToBase62String());
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            _ctx = SynchronizationContext.Current;
            GenKey();
        }

        private void btUploadTest_Click(object sender, EventArgs e)
        {
            //preparing RestRequest by adding server url, parameteres and files...
            RestRequest request = new RestRequest("upload/", Method.POST);

            request.AddParameter("up_size", "156789");
            request.AddParameter("name", "你好");
            request.AddFile("file1", @"D:\do_some_things\TCPU_UDP调试器[开源].7z");  //2M 
            //request.AddFile("file1", @"D:\Downloads\krpano-1.19-pr6-win.exe"); //67M

            //calling server with restClient
            RestClient restClient = new RestClient("http://127.0.0.1:1337/");
            restClient.Encoding = System.Text.Encoding.Default; //中文文件名问题修正
            restClient.RegisterUploadMonitor((handle, req) =>
            {
                System.Diagnostics.Debug.WriteLine("Request Body Lenghth:" + req.ContentLength.ToString());
                System.Diagnostics.Debug.WriteLine("Request HasSent :" + handle.HasSentDataLen.ToString());

                _ctx.Post((state) =>
                    {
                        probar_upload.Value = Math.Min(100, (int)((double)handle.HasSentDataLen / (double)req.ContentLength * 100.0));
                    },null);
                
            });
            restClient.ExecuteAsync(request, (response) =>
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    //POST successful
                    MessageBox.Show("Success!");
                }
                else
                {
                    //error ocured during POST
                    MessageBox.Show(":-(\nFailed.\nError: " + response.ErrorMessage);
                }
            }); ;
        }

        private void btJsonTest_Click(object sender, EventArgs e)
        {
            //preparing RestRequest by adding server url, parameteres and files...
            RestRequest request = new RestRequest("files/", Method.POST);
 
            //calling server with restClient
            RestClient restClient = new RestClient("http://127.0.0.1:1337/");
            restClient.Encoding = System.Text.Encoding.UTF8;
            

            var v = new Files() { createTime = DateTime.Now, Path = @"D:\do_some_things\", ForDownload = false, IConDownload = false };
            request.AddJsonBody(v);
            request.AddHeader("Accept", "application/json");

            restClient.ExecuteAsync<FilesResponse>(request, (response) =>
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    //POST successful
                    MessageBox.Show("Success!");
                }
                else
                {
                    //error ocured during POST
                    MessageBox.Show(":-(\nFailed.\nError: " + response.ErrorMessage);
                }
            }); 
        }

        private void lnkKeyGen_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            GenKey();
        }

    }
}
